home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / NEEDTEXT.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  275 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "needtext.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTN_NEEDTEXT"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. // Toolbar buttons. 
  108. //.................
  109. TBBUTTON tbButtons[] = 
  110.     { STD_FILENEW,  IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  111.     { STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  112.     { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  113.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0L, 0}, 
  114.     { VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  115.     { VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  116.     { VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  117.     { VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  118. }; 
  119.  
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123. static char   szTmp[80];
  124. static LPTSTR lpch;
  125. static HWND   hToolWnd   = NULL;
  126. static HWND   hStatusWnd = NULL;
  127.  
  128.    switch( uMsg )
  129.    {
  130.       case WM_CREATE :
  131.               if ( !hToolWnd )
  132.               {
  133.   
  134.                  // Initialize the common control library
  135.                  // and create the status window and toolbar.
  136.                  //..........................................
  137.                  InitCommonControls();
  138.  
  139.                  hStatusWnd = CreateStatusWindow( 
  140.                                   WS_CHILD | WS_VISIBLE | 
  141.                                   CCS_BOTTOM | SBARS_SIZEGRIP, 
  142.                                   "", hWnd, 101 );
  143.  
  144.                  hToolWnd = CreateToolbarEx( hWnd, 
  145.                                              WS_CHILD | WS_BORDER | 
  146.                                              WS_VISIBLE | TBSTYLE_TOOLTIPS, 
  147.                                              102, 
  148.                                              11,
  149.                                              (HINSTANCE)HINST_COMMCTRL, 
  150.                                              IDB_STD_SMALL_COLOR, 
  151.                                              tbButtons, 4,
  152.                                              0, 0, 100, 30, sizeof(TBBUTTON) );
  153.  
  154.                  if ( hToolWnd )
  155.                  {
  156.                     TBADDBITMAP tb;
  157.                     int index, stdidx;
  158.  
  159.                     // Add the system-defined view bitmaps.
  160.                     //.....................................
  161.                     tb.hInst = HINST_COMMCTRL;
  162.                     tb.nID = IDB_VIEW_SMALL_COLOR;
  163.                     stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 12, (LPARAM)&tb);
  164.  
  165.                     // Update the indices to the bitmaps.
  166.                     //...................................
  167.                     for (index = 4; index < 8; index++)
  168.                        tbButtons[index].iBitmap += stdidx;
  169.  
  170.                     // Add the view buttons.
  171.                     //......................
  172.                     SendMessage(hToolWnd, TB_ADDBUTTONS, 4, (LONG) &tbButtons[4]);
  173.                  }
  174.               }
  175.               break;
  176.  
  177.       case WM_NOTIFY :
  178.               switch ( ((LPNMHDR)lParam)->code )
  179.               {
  180.                  case TTN_NEEDTEXT :
  181.                       {
  182.                          LPTOOLTIPTEXT lptt = (LPTOOLTIPTEXT)lParam;
  183.  
  184.                          LoadString( hInst, lptt->hdr.idFrom, 
  185.                                      szTmp, sizeof(szTmp)-1 );
  186.  
  187.                          if ( lpch = strchr( szTmp, '\n' ) )
  188.                             lptt->lpszText = lpch+1;
  189.                       }
  190.                       break;
  191.  
  192.                  case TTN_SHOW :
  193.                       LoadString( hInst, ((LPNMHDR)lParam)->idFrom, 
  194.                                   szTmp, sizeof(szTmp)-1 );
  195.  
  196.                       if ( lpch = strchr( szTmp, '\n' ) )
  197.                          *lpch = 0;
  198.                         
  199.                       SendMessage( hStatusWnd, SB_SETTEXT, 0, (LPARAM)szTmp );
  200.                       break;
  201.  
  202.                  case TTN_POP :
  203.                       SendMessage( hStatusWnd, SB_SETTEXT, 0, (LPARAM)"" );
  204.                       break;
  205.                       
  206.               }
  207.               break;
  208.  
  209.       case WM_SIZE :
  210.               {
  211.                  RECT clrect, strect;
  212.  
  213.                  // Size the status window when the main
  214.                  // window is sized.
  215.                  //.....................................
  216.                  GetClientRect( hWnd, &clrect );
  217.                  GetClientRect( hStatusWnd, &strect );
  218.  
  219.                  MoveWindow( hStatusWnd, 0, clrect.bottom-strect.bottom, 
  220.                              LOWORD( lParam ), HIWORD( lParam ), TRUE );
  221.               }
  222.               break;
  223.  
  224.       case WM_COMMAND :
  225.               switch( LOWORD( wParam ) )
  226.               {
  227.                  case IDM_TEST :
  228.                         break;
  229.  
  230.                  case IDM_ABOUT :
  231.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  232.                         break;
  233.  
  234.                  case IDM_EXIT :
  235.                         DestroyWindow( hWnd );
  236.                         break;
  237.               }
  238.               break;
  239.       
  240.       case WM_DESTROY :
  241.               PostQuitMessage(0);
  242.               break;
  243.  
  244.       default :
  245.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  246.    }
  247.  
  248.    return( 0L );
  249. }
  250.  
  251.  
  252. LRESULT CALLBACK About( HWND hDlg,           
  253.                         UINT message,        
  254.                         WPARAM wParam,       
  255.                         LPARAM lParam)
  256. {
  257.    switch (message) 
  258.    {
  259.        case WM_INITDIALOG: 
  260.                return (TRUE);
  261.  
  262.        case WM_COMMAND:                              
  263.                if (   LOWORD(wParam) == IDOK         
  264.                    || LOWORD(wParam) == IDCANCEL)    
  265.                {
  266.                        EndDialog(hDlg, TRUE);        
  267.                        return (TRUE);
  268.                }
  269.                break;
  270.    }
  271.  
  272.    return (FALSE); 
  273. }
  274.